import { IParam, IContext, DataServiceBase, IHttpResponse } from '@/core';
{{#if appEntity.allPSAppDEMethodDTOs}}
import { {{#each appEntity.allPSAppDEMethodDTOs as | dto |}}{{pascalCase dto.codeName}}Help{{#unless @last}}, {{/unless}}{{/each}} } from '../dto-help/{{spinalCase appEntity.codeName}}-helps';
{{/if}}
import { {{pascalCase appEntity.codeName}} } from '../{{spinalCase appEntity.codeName}}';
import {{pascalCase appEntity.codeName}}Keys from '../{{spinalCase appEntity.codeName}}-keys';
{{#each appEntity.allPSAppDEMethods as | appMethod |}}
	{{#if appMethod.psSysPFPlugin}}
{{> @macro/plugins/de-method/de-method-import.hbs appPlugin=appMethod.psSysPFPlugin}}
	{{/if}}
{{/each}}

/**
 * {{appEntity.logicName}}服务对象基类
 * 基于template/src/api/\{{appEntities}}/data-service/\{{appEntities}}-service-base.ts.hbs生成
 *
 * @export
 * @class {{pascalCase appEntity.codeName}}
 * @extends {EntityBaseService}
 */
export class {{pascalCase appEntity.codeName}}ServiceBase extends DataServiceBase<{{pascalCase appEntity.codeName}}> {

	/**
     * 存储模式
     *
     * @type {(0 | 1 | 3 | 4)} 无本地存储 | 仅本地存储 | 本地及远程存储 | DTO无存储
	 * @memberof {{pascalCase appEntity.codeName}}ServiceBase
     */
	readonly storageMode: number = {{appEntity.storageMode}};

	/**
	 * Creates an instance of {{pascalCase appEntity.codeName}}ServiceBase.
	 *
	 * @param {*} [opts={}]
	 * @memberof {{pascalCase appEntity.codeName}}ServiceBase
	 */
	constructor(opts: IParam = {}) {
		super(opts, '{{pascalCase appEntity.codeName}}');
	}

	/**
	 * 初始化基础数据
	 *
	 * @protected
	 * @memberof {{pascalCase appEntity.codeName}}ServiceBase
	 */
  	protected initBasicData() {
		this.appEntityCodeName = '{{lowerCase appEntity.codeName}}';
		this.appEntityCodeNames = '{{pluralize appEntity.codeName}}';
{{#if appEntity.keyPSAppDEField}}
    	this.appEntityKeyCodeName = '{{lowerCase appEntity.keyPSAppDEField.codeName}}';
{{/if}}
{{#if appEntity.majorPSAppDEField}}
      	this.appEntityTextCodeName = '{{lowerCase appEntity.majorPSAppDEField.codeName}}';
{{/if}}
{{#if appEntity.minorPSAppDERSs}}
		this.selectContextParam = {
	{{#each appEntity.minorPSAppDERSs as | minorDers |}}
			'{{lowerCase minorDers.majorDECodeName}}': '{{#if minorDers.parentPSAppDEField}}{{lowerCase minorDers.parentPSAppDEField.codeName}}{{/if}}'{{#unless @last}},{{/unless}}
	{{/each}}
		}
{{/if}}
  	}

	/**
	 * 构造实体
	 *
	 * @protected
	 * @param {IParam} data
	 * @param {IContext} [context]
	 * @return {*} { {{pascalCase appEntity.codeName}} }
	 * @memberof {{pascalCase appEntity.codeName}}ServiceBase
	 */
	protected newEntity(data: IParam, context: IContext = {}): {{pascalCase appEntity.codeName}} {
{{#if appEntity.minorPSAppDERSs}}
	{{#each appEntity.minorPSAppDERSs as | minorDers |}}
		if (context['{{lowerCase minorDers.majorDECodeName}}'] && !data.hasOwnProperty('{{#if minorDers.parentPSAppDEField}}{{lowerCase minorDers.parentPSAppDEField.codeName}}{{/if}}')) {
      data['{{#if minorDers.parentPSAppDEField}}{{lowerCase minorDers.parentPSAppDEField.codeName}}{{/if}}'] = context['{{lowerCase minorDers.majorDECodeName}}'];
		}
	{{/each}}
{{/if}}
    return new {{pascalCase appEntity.codeName}}(data);
  }

	/**
	 * 过滤当前实体服务，标准接口数据
	 *
	 * @return {*}  {*}
	 * @memberof {{pascalCase appEntity.codeName}}ServiceBase
	 */
	protected filterEntityData(entity: {{pascalCase appEntity.codeName}}): any {
		const data: any = {};
		{{pascalCase appEntity.codeName}}Keys.forEach(key => {
				if (entity[key] !== void 0) {
						data[key] = entity[key];
				}
		});
		return data;
	}

{{#each appEntity.allPSAppDEMethods as | appMethod |}}
  {{!-- 输出非内置方法 --}}
  {{#if appMethod.builtinMethod}}
  {{else}}
    {{!-- 插件输出 --}}
    {{#if appMethod.psSysPFPlugin}}
    	{{> @macro/entity/service-plugin-method.hbs appMethod=appMethod}}
    {{else}}
      {{!-- 前台执行实体处理逻辑 --}}
      {{#and appMethod.psAppDELogic appMethod.psAppDELogic.enableFront}}
      {{/and}}
      {{!-- 自定义代码 --}}
			{{#if appMethod.customCode}}
  	{{> @macro/entity/service-script-method.hbs appMethod=appMethod}}
			{{/if}}
			{{!-- 预置集合为代码表类型 --}}
			{{#eq dataSetType 'CODELIST'}}
			{{/eq}}
			{{!-- 当前应用实体本地存储模式为仅本地存储和DTO成员（无存储）【临时模式】 --}}
			{{#or (eq appEntity.storageMode 1) (eq appEntity.storageMode 4)}}
			{{else}}
        	{{!-- 方法名不为Save和CheckKey --}}
        {{#or (neq appMethod.codeName 'Save') (neq appMethod.codeName 'CheckKey')}}
        	{{!-- 远端方法 --}}
          {{#if appMethod.requestMethod}}
  	{{> @macro/entity/service-remote-method.hbs appMethod=appMethod}}
          {{/if}}
        {{/or}}
      {{/or}}
    {{/if}}
  {{/if}}

{{/each}}
}